package com.agh.portal.web;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.agh.portal.domain.Reply;
import org.springframework.roo.addon.web.mvc.controller.finder.RooWebFinder;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/replys")
@Controller
@RooWebScaffold(path = "replys", formBackingObject = Reply.class)
@RooWebFinder
public class ReplyController {
	
    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid Reply reply, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, reply);
            return "replys/create";
        }
        uiModel.asMap().clear();
        reply.setReplyDate(new Date());
        reply.persist();
        return "redirect:/posts/" + encodeUrlPathSegment(reply.getPost().getId().toString(), httpServletRequest);
    }
}
